<template>
  <div class="sub-filter" v-if="filterDate && !filteLoadin">
    <div class="item">
      <div class="head">品牌：</div>
      <div class="body" v-if="filterDate">
        <a
        @click="changeBrand(item.id)"
          href="javascript:;"
          v-for="item in filterDate.brands"
          :key="item.id"
          :class="{active:item.id===filterDate.selectedBread}"
          >{{ item.name }}</a
        >
      </div>
    </div>
     <div
        class="item"
        v-for="item in filterDate.saleProperties"
        :key="item.id"
      >
        <div class="head">{{ item.name }}</div>
        <div class="body">
          <a
          @click="changeProp(item,minute.id)"
            href="javascript:;"
            v-for="minute in item.properties"
            :key="minute.id"
             :class="{active:minute.id===item.selectedArr}"
            >{{ minute.name }}</a
          >
        </div>
      </div>
  </div>
  <div v-else class="sub-filter">
    <XtxSkeleton class="item" width="800px" height="40px" />
    <XtxSkeleton class="item" width="800px" height="40px" />
    <XtxSkeleton class="item" width="600px" height="40px" />
    <XtxSkeleton class="item" width="600px" height="40px" />
    <XtxSkeleton class="item" width="600px" height="40px" />
  </div>
</template>
<script>
import { findSubCatetoryFilter } from '@/api/category'
import { ref, watch } from 'vue'
import { useRoute } from 'vue-router'
export default {
  name: 'SubFilter',
  setup (props, { emit }) {
    const route = useRoute()
    const filterDate = ref(null)
    const filteLoadin = ref(false)
    watch(
      () => route.params.id,
      (newValue, oldValue) => {
        if (newValue && route.path === ('/topcategory/sub/' + newValue)) {
          filteLoadin.value = true
          findSubCatetoryFilter(route.params.id).then((data) => {
            // 给所有数组前默认添加全部
            // 品牌
            data.result.selectedBread = null // 给每个对象中定义一个记录id的属性
            data.result.brands.unshift({ id: null, name: '全部' })
            // 属性
            data.result.saleProperties.forEach((item) => {
              item.selectedArr = null // 给每个对象中定义一个记录id的属性
              item.properties.unshift({ id: null, name: '全部' })
            })
            filterDate.value = data.result
            console.log(filterDate)
            filteLoadin.value = false
          })
        }
      },

      { immediate: true }
    )
    // 获取筛选参数的品牌
    const getFilterParams = () => {
      const obj = { brandId: null, attrs: [] }
      // 选择品牌
      obj.brandId = filterDate.value.selectedBread
      // 选择属性
      filterDate.value.saleProperties.forEach(item => {
        // 判断属性是否有选中值
        if (item.selectedArr) {
          const prop = item.properties.find(prop => prop.id === item.selectedArr)
          console.log(item.name)
          obj.attrs.push({ groupName: item.name, propertyName: prop.name })
        }
      })
      console.log(obj.attrs)
      // 如果attrs长度等于零，就让attrs值为空，  注意：只用null的空不会发请求，''会发请求
      if (obj.attrs.length === 0) obj.attrs = null
      return obj
    }
    // 点击品牌
    const changeBrand = (brandId) => {
      if (filterDate.value.selectedBread === brandId) return
      filterDate.value.selectedBread = brandId
      emit('filter-change', getFilterParams())
    }
    // 点击属性
    const changeProp = (item, propId) => {
      if (item.selectedArr === propId) return
      item.selectedArr = propId
      emit('filter-change', getFilterParams())
    }
    return { filterDate, filteLoadin, changeBrand, changeProp }
  }
}
</script>
<style scoped lang='less'>
// 筛选区
.sub-filter {
  background: #fff;
  padding: 25px;
  .item {
    display: flex;
    line-height: 40px;
    .head {
      width: 80px;
      color: #999;
    }
    .body {
      flex: 1;
      a {
        margin-right: 36px;
        transition: all 0.3s;
        display: inline-block;
        &.active,
        &:hover {
          color: @xtxColor;
        }
      }
    }
  }
   .xtx-skeleton {
      padding: 10px 0;
    }
}
</style>
